<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>5. User defined extension functions</title>
      <meta charset="utf-8" />
      <meta name="generator" content="DocBook XSL 2.0 Stylesheets V2.5.0" />
      <link href="css/default.css" rel="stylesheet" type="text/css" />
      <link href="css/db-prism.css" rel="stylesheet" type="text/css" />
      <link rel="stylesheet" href="css/xproc.css" type="text/css" /><script type="text/javascript" src="js/dbmodnizr.js"></script></head>
   <body>
      <div class="page">
         <div class="content">
            <div xmlns:tmpl="http://docbook.org/xslt/titlepage-templates" class="navheader">
               <table border="0" cellpadding="0" cellspacing="0" width="100%" summary="Navigation table">
                  <tr>
                     <td align="left"> <a title="XML Calabash Reference" href="index.html"><img src="img/home.png" alt="Home" border="0" /></a> <a href="extfunc.html" title="4 .  Extension functions"><img src="img/prev.png" alt="Prev" border="0" /></a> <a title="Chapter  5 .  Extensions" href="extensions.html"><img src="img/up.png" alt="Up" border="0" /></a> <a title="6 .  Extension steps" href="extsteps.html"><img src="img/next.png" alt="Next" border="0" /></a></td>
                     <td align="right"><i>XML Calabash Reference</i> (Version 0.28)</td>
                  </tr>
               </table>
            </div>
            <div class="body">
               <section id="user-extfunc" class="section">
                  <div class="section-titlepage">
                     <h2>5. User defined extension functions</h2>
                  </div>
                  <div class="content">
                     <p>When <em class="citetitle">XML Calabash</em> is used with
                        <em class="citetitle">Saxon EE</em> (and only EE, other editions do not
                        support separate compilation), you can load user defined functions
                        written in XSLT or XQuery with the <code class="tag-element">cx:import</code> extension.</p>
                     <pre class="programlisting language-none line-numbers"><code>&lt;cx:import namespace="<em class="replaceable"><code>namespace-uri</code></em>“
           href="<em class="replaceable"><code>module-file</code></em>"
           type="<em class="replaceable"><code>content-type</code></em>"/&gt;</code></pre>
                     <div class="variablelist">
                        <dl>
                           <dt><span class="term"><em class="replaceable"><code>namespace-uri</code></em></span></dt>
                           <dd>
                              <p>The module namespace declared in the library module. (This attribute
                                 is only necessary for loading XQuery functions.)</p>
                           </dd>
                           <dt><span class="term"><em class="replaceable"><code>module-file</code></em></span></dt>
                           <dd>
                              <p>The URI of the module file or stylesheet.</p>
                           </dd>
                           <dt><span class="term"><em class="replaceable"><code>content-type</code></em></span></dt>
                           <dd>
                              <p>The type of extensions you are trying to load,
                                 <code class="literal">application/xquery</code> or <code class="literal">application/xml+xslt</code>.</p>
                           </dd>
                        </dl>
                     </div>
                     <p>These extension functions are then available in XPath expressions.</p>
                     <p>For example, if <code class="filename">f.xqy</code> contains:</p>
                     <pre class="programlisting language-none line-numbers"><code>xquery version "1.0";

module namespace f="http://xmlcalabash.com/ns/functions";

declare default function namespace "http://www.w3.org/2005/xpath-functions";

declare function f:count(
  $seq as item()*
) as xs:integer
{
  count($seq)
};</code></pre>
                     <p>Then this pipeline will run:</p>
                     <pre class="programlisting language-none line-numbers"><code>&lt;p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
                xmlns:cx="http://xmlcalabash.com/ns/extensions"
                xmlns:f="http://xmlcalabash.com/ns/functions"
                xmlns:c="http://www.w3.org/ns/xproc-step"
                version="1.0" exclude-inline-prefixes="c"&gt;
   &lt;p:output port="result"&gt;
     &lt;p:pipe step="params" port="result"/&gt;
   &lt;/p:output&gt;

   &lt;cx:import namespace="http://xmlcalabash.com/ns/functions" href="f.xqy"/&gt;

   &lt;p:parameters name="params"&gt;
     &lt;p:input port="parameters"&gt;
       &lt;p:empty/&gt;
     &lt;/p:input&gt;
     &lt;p:with-param name="three" port="parameters" select="f:count((1,2,3))"/&gt;
   &lt;/p:parameters&gt;
&lt;/p:declare-step&gt;</code></pre>
                     <p>and produce output something like this:</p>
                     <pre class="programlisting language-none line-numbers"><code>&lt;c:param-set xmlns:c="http://www.w3.org/ns/xproc-step"&gt;
  &lt;c:param name="three" namespace="" value="3"/&gt;
&lt;/c:param-set&gt;</code></pre>
                     <p>At present, the dynamically loaded functions are not scoped in any way.
                        They're available globally after they've been loaded.</p>
                  </div>
               </section>
            </div>
         </div>
         <div xmlns:tmpl="http://docbook.org/xslt/titlepage-templates" class="navfooter">
            <table width="100%" summary="Navigation table">
               <tr>
                  <td width="40%" align="left"><a title="4 .  Extension functions" href="extfunc.html"><img src="img/prev.png" alt="Prev" border="0" /></a> </td>
                  <td width="20%" align="center"><a title="XML Calabash Reference" href="index.html"><img src="img/home.png" alt="Home" border="0" /></a></td>
                  <td width="40%" align="right"> <a title="6 .  Extension steps" href="extsteps.html"><img src="img/next.png" alt="Next" border="0" /></a></td>
               </tr>
               <tr>
                  <td width="40%" align="left">4 .  Extension functions </td>
                  <td width="20%" align="center"><a title="Chapter  5 .  Extensions" href="extensions.html"><img src="img/up.png" alt="Up" border="0" /></a></td>
                  <td width="40%" align="right"> 6 .  Extension steps</td>
               </tr>
            </table>
         </div>
         <div xmlns:tmpl="http://docbook.org/xslt/titlepage-templates" class="copyrightfooter">
            <p><a href="dbcpyright.html">Copyright</a> © 2011–2020 Norman Walsh.</p>
         </div>
      </div>
   </body>
</html>